package com.lz.library.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.view.*
import androidx.annotation.LayoutRes
import androidx.annotation.StyleRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.lz.library.R
import com.lz.library.extension.APP
import com.lz.library.extension.getScreenPoint

/**
 *  author: you : 2020/4/24
 */
abstract class BaseDialogFragment<B : ViewDataBinding> : DialogFragment() {

    companion object {
        //是否从activity/fragment跳转, true activity
        const val IS_FROM_ACTIVITY_OR_FRAGMENT = "is_from_activity_or_fragment"
    }

    private var _binding: B? = null
    protected val binding: B get() = _binding!!

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        _binding = DataBindingUtil.inflate(inflater, getLayoutRes(), container, false)
        _binding!!.lifecycleOwner = this
        return _binding!!.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData(view, savedInstanceState)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding?.unbind()
        _binding = null
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog =
        BaseDialog(requireContext(), R.style.lib_dialogTheme)

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        if (isFromActivity()) {
            if (context is OnDismissListener) {
                (context as OnDismissListener).onDismiss(this@BaseDialogFragment)
            }
        } else {
            if (parentFragment is OnDismissListener) {
                (parentFragment as OnDismissListener).onDismiss(this@BaseDialogFragment)
            }
        }
    }

    fun setFrom(isFromActivity: Boolean) {
        arguments?.putBoolean(IS_FROM_ACTIVITY_OR_FRAGMENT, isFromActivity)
    }

    protected fun isFromActivity(): Boolean = arguments?.getBoolean(IS_FROM_ACTIVITY_OR_FRAGMENT, false) == true

    open fun show(manager: FragmentManager) {
        super.show(manager, javaClass.simpleName)
    }

    @LayoutRes
    protected abstract fun getLayoutRes(): Int

    protected open fun initData(v: View, savedState: Bundle?) = Unit

    /**
     * @param outsideCancel 可以取消
     */
    open class BaseDialog(
        context: Context,
        @StyleRes themeResId: Int = R.style.lib_dialogTheme,
        private val outsideCancel: Boolean = true
    ) : Dialog(context, themeResId) {

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            if (!outsideCancel) setCanceledOnTouchOutside(false)
            window!!.attributes = window!!.attributes.also { initParams(it) }
        }

        override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
            if (!outsideCancel && keyCode == KeyEvent.KEYCODE_BACK) return true
            return super.onKeyDown(keyCode, event)
        }

        protected open fun initParams(params: WindowManager.LayoutParams) {
            params.width = (APP.getScreenPoint().x * 0.72f).toInt()
            params.height = WindowManager.LayoutParams.WRAP_CONTENT
        }
    }

    //底部弹窗样式, 底部弹窗一般宽度全屏
    open class BottomDialog(
        context: Context,
        outsideCancel: Boolean = true
    ) : BaseDialog(context, R.style.lib_bottomDialogTheme, outsideCancel) {

        override fun initParams(params: WindowManager.LayoutParams) {
            params.width = WindowManager.LayoutParams.MATCH_PARENT
            params.height = WindowManager.LayoutParams.WRAP_CONTENT
            params.gravity = Gravity.BOTTOM
        }
    }

    interface OnDismissListener {

        fun onDismiss(dialogFragment: BaseDialogFragment<*>)
    }
}